import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
import { visualizer } from 'rollup-plugin-visualizer'


// https://vitejs.dev/config/
export default defineConfig({
  base: './', //配置文件的根目录为相对路径
  plugins: [
    vue(),
    Components({
      resolvers: [
        AntDesignVueResolver({
          importStyle: undefined
        }),
      ],
    }),
    //查看打包体积
    // visualizer({open: true})
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    host: true,
    hmr: true
  },
  build: { 
    minify: 'terser',
    terserOptions: {
      compress: {
        //生产环境时移除console
        drop_console: true,
        drop_debugger: true,
      },
      output: {
        comments: true
      }
    },
    // rollupOptions: {
    //   output: {
    //     manualChunks: {
    //       vue: ['vue'],
    //       echarts: ['echarts']
    //     }
    //   },
    // },
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes("node_modules")) {
            // 让每个插件都打包成独立的文件
            return id .toString() .split("node_modules/")[1] .split("/")[0] .toString(); 
          }
        }
      }
    },
    
  //   关闭文件计算
    reportCompressedSize: false,
  //   关闭生成map文件 可以达到缩小打包体积
    sourcemap: false, // 这个生产环境一定要关闭，不然打包的产物会很大
},
css: {
  preprocessorOptions: {
    less: {
      javascriptEnabled: true,
    }
  }
}
})
